#include "gamepanel.h"
#include "loading.h"

#include <QPainter>
#include <QTimer>



Loading::Loading(QWidget *parent) : QWidget(parent)
{
    m_bk.load(":/images/loading.png"); //加载背景图
    setFixedSize(m_bk.size());  //设置图片大小和窗口一样大

    // 去边框
    setWindowFlags(Qt::FramelessWindowHint | windowFlags());
    // 背景透明
    setAttribute(Qt::WA_TranslucentBackground);

    QPixmap pixmap(":/images/progress.png"); //加载进度条图片
    QTimer* timer = new QTimer(this); //因为图片是动态变化的，所以添加定时器
    connect(timer, &QTimer::timeout, this, [=](){
        m_progress = pixmap.copy(0, 0, m_dist, pixmap.height()); //取出一部分进度条图片
        update();
        if(m_dist >= pixmap.width()) //进度条不能增长到最大值
        {
            timer->stop();
            timer->deleteLater();
            GamePanel* panel = new GamePanel;
            panel->show(); //把主窗口显示出来
            close(); //关掉Loading动画窗口
        }
        m_dist += 5; //每触发一次定时器，进度条就走5个像素
    });
    timer->start(15); //每隔15ms，发送timeout信号
}

void Loading::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter p(this);  //指定绘图设备，即当前窗口
    p.drawPixmap(rect(), m_bk); //把背景图画到窗口里
    p.drawPixmap(62, 417, m_progress.width(), m_progress.height(), m_progress); //画进度条
}
